<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta property="og:type" content="website">
<meta property="og:title" content="zlb技术小栈">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="zlb技术小栈">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="zlb技术小栈">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/">





  <title>zlb技术小栈</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">zlb技术小栈</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/03/音视频/音视频基本概念/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LiboZhang">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="zlb技术小栈">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2020/03/03/音视频/音视频基本概念/" itemprop="url">音视频基本概念</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-03-03T21:15:36+08:00">
                2020-03-03
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/音视频/" itemprop="url" rel="index">
                    <span itemprop="name">音视频</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>&nbsp;&nbsp;&nbsp;&nbsp;我们都知道视频是由声音和图像组成的。图像本质上就是一个个的像素点，比如我们看到的一张jpg的图片是800<em>1920,那么表示的也就是这张图片的宽是800，高是1920，然后JPEG编码是采用RGB进行编码的，我们假设一个RGB编码的像素是n,那么这张图片的大小也就是800</em>1920n。关于RGB编码常见的有RGB和ARGB。ARGB相对于RGB而言多了一个透明度的通道，每个分量占8bit,一个RGB是24bit,一个ARGB是32bit。所以一个800<em>1920的jpg图片大小就是800</em>1920<em>24/8B。<br>&nbsp;&nbsp;&nbsp;&nbsp;我们通常会说这个视频是多少帧的，这个帧表示的就是一秒钟会有多少张图片。视频本质上就是一张张的图片加上声音合成的。一个30帧的视频一秒钟就有30张图片，假如一张图片是是800</em>600*4B,那么一秒钟的图像就会占用大约54.9MB的空间，那么一分钟的图像大小就有好几个G了，好在视频在传输过程中采用的是YUV的编码方式，没有采用RGB.</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/03/音视频/FFmpeg入门/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LiboZhang">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="zlb技术小栈">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2020/03/03/音视频/FFmpeg入门/" itemprop="url">FFmpeg入门</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-03-03T20:56:37+08:00">
                2020-03-03
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/音视频/" itemprop="url" rel="index">
                    <span itemprop="name">音视频</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>&nbsp;&nbsp;&nbsp;&nbsp;FFmpeg是音视频开发经常使用的一款工具，可以说是音视频开发的瑞士军刀。FFmpeg帮我实现了很多对音视频的常见操作，这样我们在使用的过程中更多的是关于产品功能，而不是算法。确切来说，FFmpeg是一个优秀的音视频开源工具集，它把优秀的音视频算法都集成了，并且是可裁剪，开发者可以根据自己的需要编译相应的模块。FFmpeg是采用C语言实现的，因此FFmpeg有着良好的性能。当然也有其它语言的版本，其它语言的实现是建立在C语言的基础上，通过做二次封装，使得API更加简洁。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/03/Java/设计模式/建造者模式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LiboZhang">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="zlb技术小栈">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2020/03/03/Java/设计模式/建造者模式/" itemprop="url">建造者模式</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-03-03T20:13:24+08:00">
                2020-03-03
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>建造者模式是十分常见的一种设计模式，在很多开源框架中都有使用。我们需要得到一个对象，但是这个对象的初始化比较复杂，需要一系列的调用才能生成这个对象。建造者模式是实现定义好一个规范，通过这个规范来构造这个对象。<br>下面我们来用造一台电脑为例子。<br>常规实现:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">public class Computer  &#123;</span><br><span class="line"></span><br><span class="line">    public void cpu() &#123;</span><br><span class="line">    System.out.println(&quot;build cpu&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void memery() &#123;</span><br><span class="line">        System.out.println(&quot;build memery&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void motherBoard() &#123;</span><br><span class="line">        System.out.println(&quot;build mother board&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">public interface IComputer &#123;</span><br><span class="line">    public void cpu();</span><br><span class="line">    public void memery();</span><br><span class="line">    public void motherBoard();</span><br><span class="line">    public Computer getComputer();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">public class ComputerBuilder implements IComputer &#123;</span><br><span class="line">    private Computer computer;</span><br><span class="line"></span><br><span class="line">    public ComputerBuilder() &#123;</span><br><span class="line">        computer = new Computer();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public void cpu() &#123;</span><br><span class="line">        computer.cpu();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public void memery() &#123;</span><br><span class="line">        computer.memery();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public void motherBoard() &#123;</span><br><span class="line">        computer.motherBoard();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public Computer getComputer() &#123;</span><br><span class="line">        return computer;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public Computer createComputer() &#123;</span><br><span class="line">        computer.motherBoard();</span><br><span class="line">        computer.cpu();</span><br><span class="line">        computer.memery();</span><br><span class="line">        return computer;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">public class Main &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        Computer computer=new ComputerBuilder().createComputer();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>静态内部类的实现:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">public class Computer  &#123;</span><br><span class="line">    private Computer()&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    public void cpu() &#123;</span><br><span class="line">    System.out.println(&quot;build cpu&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void memery() &#123;</span><br><span class="line">        System.out.println(&quot;build memery&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void motherBoard() &#123;</span><br><span class="line">        System.out.println(&quot;build mother board&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static class Builder &#123;</span><br><span class="line">        public static Computer build()&#123;</span><br><span class="line">            Computer computer=new Computer();</span><br><span class="line">            computer.motherBoard();</span><br><span class="line">            computer.cpu();</span><br><span class="line">            computer.memery();</span><br><span class="line">            return computer;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public class Main &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        Computer computer=Computer.Builder.build();</span><br><span class="line">    &#125;</span><br><span class="line">   </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/03/Java/设计模式/观察者模式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LiboZhang">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="zlb技术小栈">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2020/03/03/Java/设计模式/观察者模式/" itemprop="url">观察者模式</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-03-03T20:13:24+08:00">
                2020-03-03
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/03/Java/设计模式/工厂模式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LiboZhang">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="zlb技术小栈">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2020/03/03/Java/设计模式/工厂模式/" itemprop="url">工厂模式</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-03-03T20:13:24+08:00">
                2020-03-03
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>1.简单工厂<br>这里以生产Computer为例子,通过传参给工厂方法不同的品牌来生成不同的computer</p>
<p>public interface IComputer {<br>    public void product();<br>}</p>
<p>public class LenovoComputer implements IComputer {<br>    @Override<br>    public void produce() {<br>        System.out.println(“produce lenovo computer”);<br>    }<br>}</p>
<p>public class XiaoMiComputer implements IComputer {<br>    @Override<br>    public void produce() {<br>        System.out.println(“produce xiaomi computer”);<br>    }<br>}</p>
<p>public class SimpleFactory {<br>    public static IComputer produce(String name) {<br>        if (“lenovo”.equals(name)) {<br>            LenovoComputer lenovoComputer = new LenovoComputer();<br>            return lenovoComputer;<br>        } else if (“xiaomi”.equals(name)) {<br>            XiaoMiComputer xiaoMiComputer = new XiaoMiComputer();<br>            return xiaoMiComputer;<br>        }<br>        return null;<br>    }</p>
<pre><code> public static void main(String[] args) {
    IComputer computer = SimpleFactory.produce(&quot;lenovo&quot;);
    computer.produce();
}
</code></pre><p>}</p>
<p>2.工厂方法<br>每当增加一种computer的时候，简单工厂的工厂方法就得修改，不遵循开闭原则。下面使用工厂方法进行改进。<br>public interface IComputer {<br>    public void produce();<br>}</p>
<p>public class LenovoComputer implements IComputer {<br>    @Override<br>    public void produce() {<br>        System.out.println(“produce lenovo computer”);<br>    }<br>}</p>
<p>public class XiaoMiComputer implements IComputer {<br>    @Override<br>    public void produce() {<br>        System.out.println(“produce xiaomi computer”);<br>    }<br>}</p>
<p>public class LennovoFactory implements IFactory {<br>    @Override<br>    public IComputer create() {<br>        return new LenovoComputer();<br>    }<br>}</p>
<p>public class XiaomiFactory implements IFactory {<br>    @Override<br>    public IComputer create() {<br>        return new XiaoMiComputer();<br>    }<br>}</p>
<p>public static void main(String[] args) {<br>        IComputer computer = new LennovoFactory().create();<br>        computer.produce();<br>    }</p>
<p>为每种computer增加一个工厂类，这样只要扩展就行了。</p>
<p>3.抽象工厂<br>抽象工厂是将工厂方法组合，比如一个工厂既要生产lenovo电脑又要生产xiaomi电脑，每个品牌的电脑又有不同的系列，如果使用前面两种工厂方法就需要在工厂方法里面加入判断逻辑，增加了耦合。所以抽象工厂是出现了。</p>
<p>public interface IComputer {<br>    public void produce();<br>}</p>
<p>public class XiaoMiComputer1 extends XiaoMiComputer {<br>    @Override<br>    public void produce() {<br>       System.out.println(“red mi”);<br>    }<br>}</p>
<p>public class XiaoMiComputer2 extends XiaoMiComputer {<br>    @Override<br>    public void produce() {</p>
<pre><code>    System.out.println(&quot;xiao mi note3&quot;);
}
</code></pre><p>}</p>
<p>public class LenovoComputer1 extends LenovoComputer {<br>    @Override<br>    public void produce() {<br>        System.out.println(“think pad”);<br>    }<br>}</p>
<p>public class LenovoComputer2 extends LenovoComputer {<br>    @Override<br>    public void produce() {<br>        System.out.println(“normal lenovo”);<br>    }<br>}</p>
<p>定义好不同Computer的工厂<br>public class XiaomiFactory implements IFactory {</p>
<pre><code>@Override
public IComputer getComputer(String name) {
    if(&quot;red mi&quot;.equals(name)){
        return new XiaoMiComputer1();
    }else if(&quot;xiao mi note3o&quot;.equals(name)){
        return new XiaoMiComputer2();
    }
    return null;
}
</code></pre><p>}</p>
<p>public class LennovoFactory implements IFactory {</p>
<pre><code>@Override
public IComputer getComputer(String name) {
    if(&quot;think pad&quot;.equals(name)){
        return new LenovoComputer1();
    }else if(&quot;normal lenovo&quot;.equals(name)){
        return new LenovoComputer2();
    }
    return null;
}
</code></pre><p>}</p>
<p>定义超级工厂，管理其它的工厂类</p>
<p>public class FactoryProducer {<br>    public static IFactory getFactory(String name) {<br>        if (“lenovo”.equals(name)) {<br>            return new LennovoFactory();<br>        } else if (“xiaomi”.equals(name)) {<br>            return new XiaomiFactory();<br>        }<br>        return null;<br>    }<br>}</p>
<p>客户端使用</p>
<p>public static void main(String[] args) {<br>        IFactory factory = FactoryProducer.getFactory(“lennovo”);<br>        IComputer computer = factory.getComputer(“think pad”);<br>        computer.produce();<br>    }</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/03/Java/设计模式/代理模式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LiboZhang">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="zlb技术小栈">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2020/03/03/Java/设计模式/代理模式/" itemprop="url">代理模式</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-03-03T20:13:24+08:00">
                2020-03-03
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>代理模式是设计模式中的一种，提供了对目标的间接访问，这样做可以实现通过代理对象实现对目标对象的访问。在生活中，也有类似的例子。比如我们需要去超市买东西，但是厂家是把商品委托给超市的，超市就成了一个代理对象，我们只要去超市就可以购买各种各样的商品了，不需要去不同的厂家一一购买。代理模式也是这样，通过一个代理对象就可以实现对目标对象的访问了。代理模式分为静态代理和动态代理。静态代理在程序运行之前关系就已经确定了，动态代理可以在运行时动态的修改。<br>1.静态代理</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">public interface IGoods &#123;</span><br><span class="line">    void sell();</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">public class Factory implements IGoods &#123;</span><br><span class="line">    @Override</span><br><span class="line">    public void sell() &#123;</span><br><span class="line">        System.out.println(&quot;我是一件商品，正在从工厂出售&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public class Proxy implements IGoods &#123;</span><br><span class="line">    @Override</span><br><span class="line">    public void sell() &#123;</span><br><span class="line">        Factory factory = new Factory();</span><br><span class="line">        factory.sell();</span><br><span class="line">        System.out.println(&quot;我是一件商品，正在从工厂出售&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public class Main &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">    Proxy proxy=new Proxy();</span><br><span class="line">    proxy.sell();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>以上就实现了一个静态代理模式，通过Proxy类代理Factory类,我们操作代理对象就可以实现对目标对象的调用</p>
<p>2.动态代理<br>当我们需要的代理目标越来越多的时候，如果使用静态代理就需要很多的代理类，因此有了动态代理，动态代理是使用反射，使用一个代理类代理所有的目标，方便管理。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">public class ProxyHandler implements InvocationHandler &#123;</span><br><span class="line">    private Object target;</span><br><span class="line"></span><br><span class="line">    public ProxyHandler(Object target) &#123;</span><br><span class="line">        super();</span><br><span class="line">        this.target = target;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable &#123;</span><br><span class="line"></span><br><span class="line">        return method.invoke(target, args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public class Main &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        Factory factory=new Factory();</span><br><span class="line">        ProxyHandler handler=new ProxyHandler(factory);</span><br><span class="line">        IGoods goods= (IGoods) Proxy.newProxyInstance(IGoods.class.getClassLoader(),new Class[]&#123;IGoods.class&#125;,handler);</span><br><span class="line">        goods.sell();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/03/Java/设计模式/单例模式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LiboZhang">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="zlb技术小栈">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2020/03/03/Java/设计模式/单例模式/" itemprop="url">单例模式</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-03-03T20:13:24+08:00">
                2020-03-03
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>单例模式是一种很常用的设计模式，很多时候，我们不需要每次都创建一个实例，全局拥有一个实例就行了，这个时候我们就需要使用设计模式了。<br>具体使用如下：<br>public class Main {<br>    /*<em>
     </em></p>
<pre><code> * SingleInstanceClass1 无锁懒汉式,单线程情况下可以使用
 * SingleInstanceClass2 有锁懒汉式,多线程情况下可以使用,效率较低
 * SingleInstanceClass3 双重锁懒汉式,多线程情况下可以使用
 * SingleInstanceClass4 饿汉式,多线程情况下可以使用,没有懒加载
 * SingleInstanceClass5 静态内部类,多线程可以使用
 */
public static void main(String[] args) {
    SingleInstanceClass1 instance1=SingleInstanceClass1.getInstance();
    SingleInstanceClass2 instance2=SingleInstanceClass2.getInstance();
    SingleInstanceClass3 instance3=SingleInstanceClass3.getInstance();
    SingleInstanceClass4 instance4=SingleInstanceClass4.getInstance();
    SingleInstanceClass5 instance5=SingleInstanceClass5.getInstance();
}
</code></pre><p>}</p>
<p>1.懒汉式(非线程安全)</p>
<p>public class SingleInstanceClass1 {<br>    private static SingleInstanceClass1 instance;</p>
<pre><code>private SingleInstanceClass1() {

}

public static SingleInstanceClass1 getInstance() {
    if (instance == null) {
        instance = new SingleInstanceClass1();

    }
    return instance;
}
</code></pre><p>}</p>
<p>2.懒汉式(同步锁，线程安全)</p>
<p>public class SingleInstanceClass2 {<br>    private static SingleInstanceClass2 instance;</p>
<pre><code>private SingleInstanceClass2() {

}

public static synchronized SingleInstanceClass2 getInstance() {
    if (instance == null) {
        instance = new SingleInstanceClass2();

    }
    return instance;
}
</code></pre><p>}</p>
<p>3.懒汉式(双重检验)</p>
<p>public class SingleInstanceClass3 {<br>    private static SingleInstanceClass3 instance;</p>
<pre><code>private SingleInstanceClass3() {

}

public static  SingleInstanceClass3 getInstance() {
    if (instance == null) {
        synchronized (SingleInstanceClass3.class){
            instance = new SingleInstanceClass3();
        }


    }
    return instance;
}
</code></pre><p>}</p>
<p>4.饿汉式</p>
<p>public class SingleInstanceClass4 {<br>    private static SingleInstanceClass4 instance=new SingleInstanceClass4();</p>
<pre><code>private SingleInstanceClass4() {

}

public static SingleInstanceClass4 getInstance() {

    return instance;
}
</code></pre><p>}</p>
<p>5.静态内部类</p>
<p>public class SingleInstanceClass5 {</p>
<pre><code>private SingleInstanceClass5() {

}

public static class SingleInstanceHolder {
    private final static SingleInstanceClass5 instance = new SingleInstanceClass5();
}

public static SingleInstanceClass5 getInstance() {

    return SingleInstanceHolder.instance;
}
</code></pre><p>}</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/03/Android/Ndk初体验/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LiboZhang">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="zlb技术小栈">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2020/03/03/Android/Ndk初体验/" itemprop="url">NDK初体验</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-03-03T20:13:24+08:00">
                2020-03-03
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index">
                    <span itemprop="name">Android</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>&nbsp;&nbsp;&nbsp;&nbsp;在Android开发中，我们一般使用Sdk提供的API进行开发，开发语言可以是Java和Kotlin。同时Android还为我们提供了NDK。NDK是什么呢？NDK是Android系统为我们提供的一个工具，通过NDK，我们可以很方便的调用C++代码。我们知道不同的Android设备之间差异性很大。它们的底层是有很大差异的，比如arm和x86,平台不一致导致驱动程序是不一样的。这样我们在调用不同平台的时候就得写很多套代码，NDK给我们提供了一个交叉编译的功能，可以同时编译支持多个平台的代码。<br>另外C++的运行效率是比较高的，所以一些对性能要求比较高的地方就可以使用NDK开发。利用NDK我们也可以很方便操作一些底层API,NDK编译出来的是二进制文件，反编译比较困难，从代码安全的角度上来说是比较安全的。<br>总的来说NDK有一下几个优点：<br>1.高性能<br>2.方便的操作底层<br>3.反编译难度大，代码安全<br>&nbsp;&nbsp;&nbsp;&nbsp;但是NDK开发的难度比较大，所以一般只是部分模块使用NDK。<br>下面我们开始使用NDK开发。<br>1.搭建环境，安装Android Studio，利用SDK Manager安装NDK,CMAKE,LLDB。<br>2.新建项目。在新建项目的时候勾选C++支持就可以直接创建了。对于已有项目需要按后续操作添加配置<br>3.在module目录下创建CMakeLists.txt文件，</p>
<pre><code>#cmake最低版本
cmake_minimum_required(VERSION 3.4.1) 
add_library( 
        # 库的名称
         native-lib
        # 库的类型
         SHARED

    # NDK源文件路径
    src/main/cpp/native-lib.cpp)
    find_library( # Sets the name of the path variable.
          log-lib

          # Specifies the name of the NDK library that
          # you want CMake to locate.
          log )
    target_link_libraries( # Specifies the target library.
                   native-lib

                   # Links the target library to the log library
                   # included in the NDK.
                   ${log-lib} )
</code></pre><p>4.在main目录下创建cpp目录<br>5.添加gradle配置<br>defaultConfig{<br> externalNativeBuild {<br>            cmake {<br>                cppFlags “-std=c++14”<br>                abiFilters ‘arm64-v8a’,’armeabi-v7a’,’x86’,’x86_64’<br>            }<br>        }<br>}</p>
<p>android{<br>     externalNativeBuild {<br>        cmake {<br>            path “CMakeLists.txt”<br>        }<br>    }<br>}</p>
<p>6.编写代码<br>1）Java层代码<br>package com.zlb.ndkdemo;</p>
<p>import android.support.v7.app.AppCompatActivity;<br>import android.os.Bundle;<br>import android.view.View;<br>import android.widget.Button;<br>import android.widget.Toast;</p>
<p>public class MainActivity extends AppCompatActivity {</p>
<pre><code>private Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, getString(), Toast.LENGTH_LONG).show();
        }
    });

}

static {
    System.loadLibrary(&quot;native-lib&quot;);
}
public native String getString();
</code></pre><p>}</p>
<p>2）cpp代码</p>
<p>#include &lt;jni.h&gt;</p>
<p>#ifdef __cplusplus<br>extern “C” {</p>
<p>JNIEXPORT jstring JNICALL Java_com_zlb_ndkdemo_MainActivity_getString(JNIEnv <em>env, jobject type) {<br>//    return (</em>env)-&gt;NewStringUTF(env,”aaaa”);<br>    return env-&gt;NewStringUTF(“aaa”);<br>}<br>}</p>
<p>#endif<br>7.运行一下就能看到效果了</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/03/Java/java四种引用/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LiboZhang">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="zlb技术小栈">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2020/03/03/Java/java四种引用/" itemprop="url">Java四种引用</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-03-03T20:13:24+08:00">
                2020-03-03
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>java和c++最典型的区别就是内存的管理方式不同，java的内存管理是通过虚拟机来实现的，通过垃圾回收器释放内存。c++通常情况下是通过程序员手动释放。为什么说是通常呢？在c++有智能指针，通过引用计数方式释放内存。</p>
<p>在c++11中，智能指针分为强引用指针、强指针引用和弱引用指针。强引用指针每个指针独享一块内存区域、强指针引用引用是大家共享一块堆内存，弱引用指针很多时候在配合强指针引用使用，动态的知道所指的对象是否活着。还有就是解决循环引用的问题。java的引用类型和c++的智能指针有一定相似的地方。</p>
<p>java中的引用类型分为强引用、弱引用、虚引用和软引用四种。</p>
<p>强引用</p>
<p>Object object=new Object();</p>
<p>这是java通常的创建对象的方式,jvm不会回收强引用所引用的对象，即使内存不足，发生OOM错误。</p>
<p>如果将object=null;那么在下次触发gc的时候我们new出来的这个对象就会被销毁。</p>
<p>弱引用</p>
<p>无论内存是否充足，在进行垃圾回收的时候，jvm都会将弱引用所引用的对象回收</p>
<p>String str=new String(“hello world”);<br>WeakReference<string> sf = new WeakReference<string>(str);<br>str=null;<br>System.out.println(sf.get());<br>System.gc();<br>System.out.println(sf.get());<br>首先会输出hello world,然后输出null。在进行垃圾回收的时候，若引用关联的对象会被回收。<br>软引用<br>如果一个对象被软引用所引用，那么在内存不足的情况下，会被jvm回收。</string></string></p>
<p> String str=new String(“hello world”);<br>       SoftReference<string>sr=new SoftReference<string>(str);<br>       List<object>data=new ArrayList&lt;&gt;();<br>       while (true){<br>           System.out.println(sr.get());<br>           data.add(new String[1024]);<br>       }<br>这段程序刚开始输出的是hello world,一段时间后输出null，最后发生OOM错误退出。jvm参数设置不同，效果有所区别。当内存不够时，jvm会对软引用进行回收。</object></string></string></p>
<p>虚引用</p>
<p>如果一个引用和虚引用关联，则和没有引用关联一样，jvm会将其回收。虚引用需要和引用队列联合使用，需要将虚引用加入引用队列中，如果虚引用已加入引用队列，那么会被虚引用引用的对象将会被回收。<br>ReferenceQueue<string>queue=new ReferenceQueue&lt;&gt;();<br>String str=new String(“hello world”);<br>PhantomReference<string>ph=new PhantomReference&lt;&gt;(str,queue);<br>System.out.println(ph.get());</string></string></p>
<p>这里会输出null。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/03/Android/leakcanary内存检测/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LiboZhang">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="zlb技术小栈">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2020/03/03/Android/leakcanary内存检测/" itemprop="url">使用LeakCanary检测内存泄漏</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-03-03T20:13:24+08:00">
                2020-03-03
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index">
                    <span itemprop="name">Android</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>&nbsp;&nbsp;&nbsp;&nbsp;LeakCanary是一个内存泄漏检测工具，它可以在我们的应用程序发生内存泄漏的时候进行提醒。一般在开发期间，通过打印日志的方式告诉开发者。应用上线后，如果发生内存泄漏，则将警告发送到后台服务器。</p>
<h4 id="LeakCanary使用步骤如下LeakCanary"><a href="#LeakCanary使用步骤如下LeakCanary" class="headerlink" title="LeakCanary使用步骤如下LeakCanary"></a>LeakCanary使用步骤如下<a href="https://github.com/square/leakcanary" target="_blank" rel="noopener">LeakCanary</a></h4><p>1)添加依赖:<br>dependencies {<br>  debugImplementation ‘com.squareup.leakcanary:leakcanary-android:1.6.3’<br>  releaseImplementation ‘com.squareup.leakcanary:leakcanary-android-no-op:1.6.3’<br>  // Optional, if you use support library fragments:<br>  debugImplementation ‘com.squareup.leakcanary:leakcanary-support-fragment:1.6.3’<br>}</p>
<p>2)在Application中初始化</p>
<p>public class MyApplication extends MultiDexApplication {</p>
<p>  @Override public void onCreate() {<br>    super.onCreate();<br>    if (LeakCanary.isInAnalyzerProcess(this)) {<br>      // This process is dedicated to LeakCanary for heap analysis.<br>      // You should not init your app in this process.<br>      return;<br>    }<br>    LeakCanary.install(this);</p>
<p>  }<br>}</p>
<p>3)查看leakcanary打印的日志，如果存在泄漏，会打印出日志</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  


          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">LiboZhang</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LiboZhang</span>

  
</div>









        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
